/*
 * @Author: mikey.nibote 
 * @Date: 2018-09-06 13:57:03 
 * @Last Modified by: mikey.nibote
 * @Last Modified time: 2018-09-20 17:23:07
 */

 module.exports = ()=>{
     return async function errorHandler(ctx,next){
         try{
             await next()
         } catch(err){
             ctx.app.emit("error",err,ctx);
             const status = err.status || 500;
             const error = status === 500 && ctx.app.config.env === 'prod' ? '服务器内部错误' : err.message;

             ctx.body = {error};
             if(status === 422){
                 ctx.body.detail = err.errors;
             }
             ctx.status = status;
         }
     }
 }